package com.fingard.dsp.bank.directbank.czcb01;

import com.fingard.dsp.bank.directbank.bankInterface.IBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.ReqBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.ReqQryBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.RetAtsBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.RetAtsQryBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.detail.RetAtsBatPayRecDetail;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * @ClassName CZCB01BatPayRec
 * @Author Zhengzy
 * @Date 2019/12/24 10:32
 * @Version 1.0
 **/
public class CZCB01BatPayRec extends CZCB01Base implements IBatPayRec {
    @Override
    public RetAtsBatPayRec d_1988_BatPay(ReqBatPayRec p_req1988) throws Exception {
        RetAtsBatPayRec retAts = new RetAtsBatPayRec(p_req1988.reqHeader);
        retAts.writeStart();
        if (bankFront.userName == null || bankFront.userName == "") {
            retAts.writeRespCode("-1");
            retAts.writeRespInfo("无客户号节点或未配置客户号！");
            retAts.writeEnd();
            return retAts;
        }
        try {
            // 报文组装
            XElement stream = new XElement("stream", "");// 内层明文
            XmlTextWriter outTextWriter = new XmlTextWriter();// 外层报文

            outTextWriter.writeStartDocByDeclaration("<?xml version=\"1.0\" encoding=\"GBK\"?>");
            outTextWriter.writeStartElement("CZBANK");

            stream.addChild(new XElement("action", "BC0301"));
            stream.addChild(new XElement("userName", bankFront.org));
            stream.addChild(new XElement("clientID", p_req1988.reqHeader.reqSeqID));// 客户流水号（批次号）
            stream.addChild(new XElement("totalNumber", p_req1988.totalNum));// 总笔数 2000以下，超出分批
            stream.addChild(new XElement("totalAmount", p_req1988.totalAmt));// 总金额
            stream.addChild(new XElement("payAccountNo", p_req1988.ownAct));// 付款账号
            stream.addChild(new XElement("abstract", ""));// 摘要
            XElement list = new XElement("lsit", "");
            list.addAttr("name", "userDataList");
            int tmpCount = 0;
            while (p_req1988.readRecord()) {
                tmpCount++;
                XElement row = new XElement("row", "");// 每一条代发工资的工资信息
                row.addChild(new XElement("seqNo", p_req1988.readedDetail.askNumber));// 交易序号
                row.addChild(new XElement("employeeName", p_req1988.readedDetail.recName));// 员工姓名
                row.addChild(new XElement("recAccountNo", p_req1988.readedDetail.recAct));// 收款账号
                row.addChild(new XElement("tranAmount", p_req1988.readedDetail.payAmount));// 金额
                list.addChild(row);
            }
            stream.addChild(list);// 把所有请求组装到stream中，构成明文
            outTextWriter.writeXElement(stream); // 把明文组装入报文
            outTextWriter.writeElementString("signData", "加签加密内容");// 把密文组装入报文
            outTextWriter.writeEndElement();

            // 发送给银行
            String[] bankResult = sendToBank(outTextWriter.toXmlString());
//            String[] bankResult = new String[]{"", "<?xml version=\"1.0\" encoding=\"GBK\"?>\n" +
//                    "<CZBANK>\n" +
//                    "<stream>\n" +
//                    "<status>0000</status>\n" +
//                    "\t\t<statusText>发起批量支付</statusText>\n" +
//                    "\t\t<totalNumber>2</totalNumber>\n" +
//                    "\t\t<totalAmount>20000.0</totalAmount>\n" +
//                    "</stream>\n" +
//                    "<signData>密文</signData>\n" +
//                    "</CZBANK>\n"};

            if (bankResult[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(bankResult[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmpRetCode = tmpBkReader.getSimpleText("status");// 交易状态
                    String tmpRetInfo = tmpBkReader.getSimpleText("statusText");// 交易状态信息
                    retAts.respCode = "0";
                    retAts.respInfo = tmpRetInfo;
                    if ("0000".equals(tmpRetCode)) {
                        retAts.reqSeqID = p_req1988.reqBatchNO;
                        retAts.transState = tmpRetCode;// 映射成银行处理中，状态可由查询查看
                        retAts.payInfoCode = tmpRetCode;
                    } else {
                        retAts.respCode = "-1";
                    }
                } else {
                    retAts.respCode = "-1";
                    retAts.respInfo = tmpSbMsg.toString();
                }
            } else {
                retAts.respCode = "-1";
                retAts.respInfo = bankResult[1];
            }

        } catch (Exception e) {
            retAts.respCode = "-1";
            retAts.respInfo = e.getMessage();
        }
        retAts.writeRetToATS();
        return retAts;
    }

    @Override
    public RetAtsQryBatPayRec d_1998_QryBatPay(ReqQryBatPayRec p_req1998) throws Exception {
        // 返回给ATS的对象
        RetAtsQryBatPayRec retAts = new RetAtsQryBatPayRec(p_req1998.reqHeader);
        retAts.writeStart();
        if (bankFront.userName == null || bankFront.userName == "") {
            retAts.writeRespCode("-1");
            retAts.writeRespInfo("无客户号节点或未配置客户号！");
            retAts.writeEnd();
            return retAts;
        }
        try {
            // 组装上送报文
            XElement stream = new XElement("stream", "");// 内层明文
            XmlTextWriter outTextWriter = new XmlTextWriter();// 外层报文

            outTextWriter.writeStartDocByDeclaration("<?xml version=\"1.0\" encoding=\"GBK\"?>");
            outTextWriter.writeStartElement("CZBANK");

            stream.addChild(new XElement("action", "BC0302"));
            stream.addChild(new XElement("userName", bankFront.org));
            stream.addChild(new XElement("clientID", p_req1998.reqHeader.reqSeqID));

            // 对明文报文进行加签加密  对xmlTextWriter进行加签加密
            //String encryptData = stream.加签加密;

            outTextWriter.writeXElement(stream); // 把明文组装入报文
            outTextWriter.writeElementString("signData", "加签加密内容");// 把密文组装入报文
            outTextWriter.writeEndElement();


            // 发送给银行
            String[] bankResult = sendToBank(outTextWriter.toXmlString());
//            String[] bankResult = new String[]{"", "<?xml version=\"1.0\" encoding=\"GBK\"?>\n" +
//                    "<CZBANK>\n" +
//                    "<stream>\n" +
//                    "<status>0000</status>\n" +
//                    "\t\t<statusText>批量支付查询</statusText>\n" +
//                    "<payAccountNo>55555</payAccountNo>\n" +
//                    "<totalRecords>2</totalRecords>\n" +
//                    "<totalAmount>2222.00</totalAmount>\n" +
//                    "\t\t<list name=\"userDataList\">\n" +
//                    "\t\t\t<row>\n" +
//                    "                 <seqNo>44</seqNo>\n" +
//                    "\t\t      \t<employeeName>郑章洋</employeeName>\n" +
//                    "\t\t       \t<recAccountNo>67867</recAccountNo>\n" +
//                    "<tranAmount>12000.00</tranAmount>\n" +
//                    "<status>00000</status>\n" +
//                    "<statusText>交易状态信息</statusText>\n" +
//                    "\t\t\t</row>\n" +
//                    "\t\t\t<row>\n" +
//                    "                 <seqNo>777</seqNo>\n" +
//                    "\t\t       \t<employeeName>张三</employeeName>\n" +
//                    "\t\t       \t<recAccountNo>4444</recAccountNo>\n" +
//                    "<tranAmount>8000.00</tranAmount>\n" +
//                    "<status>0000</status>\n" +
//                    "<statusText>交易状态信息</statusText>\n" +
//                    "\t\t\t</row>\n" +
//                    "\t\t</list>\n" +
//                    "</stream>\n" +
//                    "<signData>密文</signData>\n" +
//                    "<CZBANK>\n"};
            if (bankResult[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(bankResult[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmpRetCode = tmpBkReader.getFirstTextNotNull("/CZBANK/stream/status");// 交易状态
                    String tmpRetInfo = tmpBkReader.getSimpleText("statusText");// 交易状态信息
                    if ("0000".equals(tmpRetCode)) {
                        retAts.respCode = "0";
                        retAts.respInfo = tmpRetInfo;
                        retAts.reqSeqID = p_req1998.reqBatchNO;
                        retAts.writeBeforeDetail();
                        while (tmpBkReader.read("/CZBANK/stream/list/row")) {
                            RetAtsBatPayRecDetail tmpRetDetail = new RetAtsBatPayRecDetail();
                            tmpRetDetail.payAct = tmpBkReader.readedXElement.getSingleText("payAccountNo");// 付款账号
                            tmpRetDetail.recAct = tmpBkReader.readedXElement.getSingleText("recAccountNo");// 收款账号
                            tmpRetDetail.recName = tmpBkReader.readedXElement.getSingleText("employeeName");// 员工姓名
                            tmpRetDetail.postScript = tmpBkReader.readedXElement.getSingleText("seqNo");// 交易流水号
                            tmpRetDetail.payAmount = tmpBkReader.readedXElement.getSingleText("tranAmount");// 金额
                            String statusCode = tmpBkReader.readedXElement.getSingleText("status");// 每个员工的交易状态0000成功
                            // String statusText = tmpBkReader.readedXElement.getSingleText("statusText");// 每个员工的交易信息
                            tmpRetDetail.result = statusCode;// 支付状态码
                            tmpRetDetail.payInfoCode = statusCode;// 支付信息码
                            retAts.writeRetDetail(tmpRetDetail);
                        }
                    } else {
                        retAts.respCode = "-1";
                        retAts.respInfo = tmpRetInfo;
                        RetAtsBatPayRecDetail tmpRetDetail = new RetAtsBatPayRecDetail();
                        tmpRetDetail.result = "PaymentFailed";// 支付状态码
                        tmpRetDetail.payInfoCode = "PaymentFailed";// 支付信息码
                        retAts.writeRetDetail(tmpRetDetail);
                    }
                } else {
                    retAts.respCode = "-1";
                    retAts.respInfo = bankResult[1];
                }
            }
        } catch (Exception e) {
            retAts.respCode = "-1";
            retAts.respInfo = e.getMessage();
        }

        retAts.writeAfterDetail();
        return retAts;
    }

    @Override
    public RetAtsBatPayRec d_9188_BatPay(ReqBatPayRec p_req9188) throws Exception {
        return null;
    }

    @Override
    public RetAtsQryBatPayRec d_9198_QryBatPay(ReqQryBatPayRec p_req9198) throws Exception {
        return null;
    }
}
